package com.kaili.basic.service.impl;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.PickManageMapper;
import com.kaili.basic.pojo.dto.WellenAndPickManagedto;
import com.kaili.basic.pojo.dto.Wellendto;
import com.kaili.basic.pojo.entity.PickManage;
import com.kaili.basic.pojo.entity.Wellen;
import com.kaili.basic.pojo.entity.WellenPick;
import com.kaili.basic.pojo.entity.WellenProduct;
import com.kaili.basic.pojo.vo.OutApplicationFormVO;
import com.kaili.basic.pojo.vo.OutApplicationProductVO;
import com.kaili.basic.pojo.vo.WellenVo;
import com.kaili.basic.service.PickManageService;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.*;

@Service
@Transactional
public class PickManageServiceImpl implements PickManageService {



    @Autowired
    private PickManageMapper pickManageMapper;

    /*查询出库申请单*/
    public Page<PickManage> queryOutApplicationByPage(Page<PickManage> pages, PickManage pickManage){
        pages.setRecords(pickManageMapper.queryOutApplicationByPage(pages,pickManage));
        System.out.println(pages);
        return pages;
    }


    /*根据出库申请单ID查询产品列表*/
    public List<OutApplicationProductVO> queryOutApplicationProduct(Integer applyId){
        return pickManageMapper.queryOutApplicationProduct(applyId);
    }


    /*生成波次单修改出库申请单*/
    public void createWellen(List<WellenAndPickManagedto> pickManages){
        //生成波次单
        Wellen wellen = new Wellen();
        //获取波次单号(利用序列和补零)
        int number = pickManageMapper.getWellenNum();
        wellen.setWellenNo(String.format("%010d", number));
        wellen.setShipperId(pickManages.get(0).getShipper());
        wellen.setDepotId(pickManages.get(0).getDepotId());
        wellen.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        wellen.setCreateTime(new Date());
        wellen.setRemark("/");
        wellen.setStatus(0);//(0:创建,1:已确认,2:分配中,3:拣货中,4:拣货完成,5:二次分拣中,6完成)
        wellen.setRdStatus(1);
        pickManageMapper.createWellen(wellen);
        for (WellenAndPickManagedto wellenAndPickManagedto:pickManages
             ) {

            for (OutApplicationProductVO outApplicationProductVO:queryOutApplicationProduct(wellenAndPickManagedto.getApplyId())
                 ) {
                WellenProduct wellenProduct = new WellenProduct();
                //将产品添加到波次单产品列表
                wellenProduct.setRdStatus(1);
                wellenProduct.setProductId(outApplicationProductVO.getId());
                wellenProduct.setWellenId(wellen.getWellenId());
                wellenProduct.setSpec(outApplicationProductVO.getSpec());
                wellenProduct.setUnit(outApplicationProductVO.getUnit());
                wellenProduct.setNumber(outApplicationProductVO.getNumber());
                wellenProduct.setWeight(outApplicationProductVO.getWeight());
                wellenProduct.setVol(outApplicationProductVO.getVol());
                wellenProduct.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
                wellenProduct.setCreateTime(new Date());
                pickManageMapper.createWellenProduct(wellenProduct);
            }
            //修改出库申请单波次ID
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("applyId",wellenAndPickManagedto.getApplyId());
            params.put("wellenId",wellen.getWellenId());
            updateOutApplication(params);
        }




    }


    public void updateOutApplication(Map<String,Object> params){
        pickManageMapper.updateOutApplication(params);
    }

    /*根据出库申请单ID查询是否存在波次单号*/
    public Integer getWellenNoByApplyId(Integer applyId){
        return pickManageMapper.getWellenNoByApplyId(applyId);
    }


    /*根据分页条件查询波次单*/

    public Page<WellenVo> queryWellenByPages(Page<WellenVo> pages, WellenVo wellenVo){
        return pages.setRecords(pickManageMapper.queryWellenByPages(pages,wellenVo));
    }

    /*根据导出条件查询波次单*/

    public List<WellenVo> queryWellenByExport(WellenVo wellenVo){
        return pickManageMapper.queryWellenByExport(wellenVo);
    }


    /*根据波次单ID删除波次单*/
    public void deleteWellenByWellenId(Long wellenId){
        //删除波次单产品
        pickManageMapper.deleteWellenProductByWellenId(wellenId);
        //删除波次单拣货单
        pickManageMapper.deleteWellenPickByWellenId(wellenId);
        //删除波次单
        pickManageMapper.deleteWellenByWellenId(wellenId);
    }

    /*根据波次单ID修改波次单状态*/
    public void updateWellenById(Wellendto wellendto){
        pickManageMapper.updateWellenById(wellendto);
    }

    public void updateWellen(Wellendto wellendto){
        if (wellendto.getWellenStatus() == 2){
            //分配按钮,创建拣货单
            List<WellenProduct> wellenProducts = pickManageMapper.getWellenProductByWellenId(wellendto.getWellenId());
            Integer depotId = pickManageMapper.getWellenById(wellendto.getWellenId());
            Map<String,Object> params = new HashMap<String,Object>();
            for (WellenProduct wellenProduct:wellenProducts
                 ) {
                WellenPick wellenPick = new WellenPick();
                wellenPick.setWellenId(wellenProduct.getWellenId());
                wellenPick.setProductId(wellenProduct.getProductId());
                wellenPick.setSpec(wellenProduct.getSpec());
                wellenPick.setUnit(wellenProduct.getUnit());
                wellenPick.setNumber(wellenProduct.getNumber());
                wellenPick.setVol(wellenProduct.getVol());
                wellenPick.setWeight(wellenProduct.getWeight());
                wellenPick.setDepotId(depotId);
                wellenPick.setRdStatus(1);
                //根据产品ID和仓库ID查询库位ID
                params.clear();
                params.put("storeId",depotId);
                params.put("productId",wellenProduct.getProductId());
                wellenPick.setLocatorId(pickManageMapper.getlocatorIdByDepotId(params));
                pickManageMapper.createWellenPick(wellenPick);

            }
            //修改波次单
            wellendto.setPickPerson(LoginDetailUtils.getCurrentEmployeeId());
            updateWellenById(wellendto);
        }else {
            //修改状态
            updateWellenById(wellendto);
        }
    }


    /*二次分拣根据分页条件查询波次单*/

    public Page<WellenVo> queryTwoWellenByPages(Page<WellenVo> pages, WellenVo wellenVo){
        return pages.setRecords(pickManageMapper.queryTwoWellenByPages(pages,wellenVo));
    }

    /*二次分拣根据条件查询波次单*/
    public List<WellenVo> queryTwoWellenByExcport(WellenVo wellenVo){
        return pickManageMapper.queryTwoWellenByExcport(wellenVo);
    }

    /*波次id查询正在进行中的操作*/
    public Integer getWellenOperationCount(Integer welleId){
        return pickManageMapper.getWellenOperationCount(welleId);
    }
}
